﻿@using eLoan.Models;
@model ReleaseToBranchModel

@{
    ViewBag.Title = "Release to Branch";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Release to Branch</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li class="active">
                <strong>Release to Branch</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
            @using (Html.BeginForm("ReleaseToBranch", "Funding", FormMethod.Post, new { id = "saveForm" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)
                
                <fieldset>
                    <div class="ibox-title">
                        <div class="divSearch">
                            <table class="table-search">
                                <tr>
                                    <td class="tdFixed">
                                        <span>@Html.LabelFor(m => m.Branches)</span>
                                        @Html.DropDownListFor(m => m.SelectedBranchCode, Model.Branches, new { @id = "ddlBranches", @class = "text-box single-line" })
                                    </td>
                                    <td class="tdFixed">
                                        @Html.TextBoxFor(m => m.SearchText, new { @class = "single-line" })
                                    </td>
                                    <td class="tdFixed">
                                        <button type="submit" id="btnRefresh" class="btn btn-primary btnSearch" name="command" value="Refresh">Search</button>
                                    </td>
                                    <td class="tdAlignRight">
                                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#confirm-release">Release to branch</button>
                                        <button type="submit" id="btnSave" style="display:none" name="command" value="Save">Release to branch</button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="divScroll">
                            <table id="tbl" class="table table-bordered">
                                <thead class="k-grid-header">
                                    <tr>
                                        <th class="k-header" style="width:150px">
                                            <span class="k-link">Document No</span>
                                        </th>
                                        <th class="k-header" >
                                            <span class="k-link">Borrower</span>
                                        </th>
                                        <th class="k-header" >
                                            <span class="k-link">Agent</span>
                                        </th>
                                        <th class="k-header" style="width:120px">
                                            <span class="k-link">Loan Amount</span>
                                        </th>
                                        <th class="k-header" style="width:120px">
                                            <span class="k-link">Loan Interest %</span>
                                        </th>
                                        <th class="k-header" style="width:120px">
                                            <span class="k-link">Receivable</span>
                                        </th>
                                        <th class="k-header" style="width:120px">
                                            <span class="k-link">Total Deduction</span>
                                        </th>
                                        <th></th>
                                        <th >
                                            @Html.CheckBox("chkCheckAll", false, new { @class = "checkAll", @onchange = "checkAll(this)" })
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="tableBody">
                                    @for (var i = 0; i < Model.ReleaseToBranchItems.Count(); i++)
                                    {
                                    <tr class="trData">
                                        <td>
                                            @Html.ActionLink(Model.ReleaseToBranchItems[i].DocumentNo, "LoanDetails", "Loan", null, null, null, new { documentNo = Model.ReleaseToBranchItems[i].DocumentNo }, null)
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].DocumentNo)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].BorrowerName)
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].BorrowerName)
                                        </td>
                                        <td>
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].AgentName)
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].AgentName)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].LoanAmount, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].LoanAmount)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].LoanInterest, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].LoanInterest)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].Receivable, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].Receivable)
                                        </td>
                                        <td style="text-align:right">
                                            @Html.ValueFor(m => m.ReleaseToBranchItems[i].TotalDeduction, "{0:#,###,##0.00}")
                                            @Html.HiddenFor(m => m.ReleaseToBranchItems[i].TotalDeduction)
                                        </td>
                                        <td>
                                            @Html.ActionLink("View schedule", "GenerateSchedule", "Funding", new { documentNo = Model.ReleaseToBranchItems[i].DocumentNo, source = "ReleaseToBranch" }, null)
                                        </td>
                                        <td>
                                            @Html.CheckBoxFor(m => m.ReleaseToBranchItems[i].IsSelected, new { @id = "chk_" + Model.ReleaseToBranchItems[i].DocumentNo, @class = "check", @onchange = "checkMe();" })
                                        </td>
                                    </tr>
                                    }
                                </tbody>
                            </table>
                        </div>

                        <div class="modal fade" id="confirm-release" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">

                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel">Confirmation</h4>
                                    </div>

                                    <div class="modal-body">
                                        <p>Are you sure you want to release the selected documents to branch?</p>
                                    </div>

                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
                                        <button type="button" class="btn btn-primary btn-ok" data-dismiss="modal">Yes</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </fieldset>
            }
            </div>

        </div>
    </div>
</div>

@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")

    <style>

    .divScroll {
            height: 400px;
            overflow-x: scroll;
            border: solid 1px #EBEBEB;
        }

    select[disabled], select[disabled]:hover {
        pointer-events: none;
        color: #e1e1e1;
    }

    .divSearch {
        background-color: #F5F5F6;
        border: 1px solid #EBEBEB;
        margin-top: 5px;
    }

    .table-search {
        width: 100%;
        border: none;
    }

        .table-search td {
            margin: 10px 10px 10px 10px;
            padding: 10px 10px 10px 10px;
            vertical-align: bottom;
        }

        .table-search .tdAlignRight {
            text-align: right;
        }

        .table-search .tdFixed {
            width: 20%;
        }

            .table-search .tdFixed div {
                margin-bottom: 0px;
            }

        .table-search td .btnSearch {
            margin-bottom: 0px;
        }


        .table-search td span {
            margin-left: 7px;
        }
</style>

}

@section Scripts {
    @*@Scripts.Render("~/bundles/jqueryval")*@

    <script id="onDocumentReady" type="text/javascript">

        $(document).ready(function () {

            $(window).keydown(function (event) {
                if (event.keyCode == 13) {
                    Refresh();
                    return false;
                }
            });

            $('#confirm-release').on('click', '.btn-ok', function (e) {
                document.getElementById('btnSave').click();
            });

        });

    </script>

    <script id="methods" type="text/javascript">

        function Refresh() {
            document.getElementById('btnSearch').click();
        }

        function checkAll(me) {
            var val = $(me).is(':checked');

            $('.check').each(function () {
                $(this).prop('checked', val);
            });

            $('.prnt').each(function () {
                
                if (val) {
                    $(this).removeAttr('disabled');
                }
                else {
                    $(this).attr('disabled', 'disabled');
                }
                
            });


        }

        function checkMe() {
            var allIsChecked = true;

            $('.check').each(function () {
                var val = $(this).is(':checked');

                if (!val) 
                    allIsChecked = false;
                    
            });

            $('#chkCheckAll').prop('checked', allIsChecked);

        }

    </script>
}
